﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WPFByCoreApp
{
    public class TestConcurrentDictionary
    {
        public ConcurrentDictionary<string, KeyValuePair<string, object>> InsideMessageQueues =
    new ConcurrentDictionary<string, KeyValuePair<string, object>>();

        public string RightSweepSetupEditSegmentInitListControlData = "RightSweepSetupEditSegmentInitListControlData";

        private void AddMessageQueues(KeyValuePair<string,
     KeyValuePair<string, object>> messageValue, bool isInsideSign = false,
     bool isProgress = false, bool isForceInsert = false)
        {
            try
            {

                // if (Monitor.TryEnter(CommonData.InsideMessageQueues, 300))
                {
                    if (!InsideMessageQueues.ContainsKey(messageValue.Key))
                    {
                        //if (Monitor.TryEnter(CommonData.InsideMessageQueues, 100))
                        {
                            InsideMessageQueues.TryAdd(messageValue.Key, messageValue.Value);
                        }
                    }
                }

            }
            catch (Exception)
            {
            }
        }


        private void ExecuteInsideMessageQueues()
        {
            KeyValuePair<string, KeyValuePair<string, object>> pair;
            KeyValuePair<string, object> value;

            while (true)
            {
                try
                {
                    if (InsideMessageQueues.Count > 0)
                    {


                        // if (Monitor.TryEnter(CommonData.InsideMessageQueues, 300))
                        {
                            //pair = InsideMessageQueues.First();
                            //InsideMessageQueues.TryRemove(pair.Key, out value);

                            System.Diagnostics.Debug.WriteLine("InsideMessageQueues:" + InsideMessageQueues.Count);

                        }
                    }


                    Thread.Sleep(2);
                }
                catch (Exception)
                {
                }
            }
        }


        public TestConcurrentDictionary()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(par =>
            {
                while (true)
                {
                    AddMessageQueues(new KeyValuePair<string, KeyValuePair<string, object>>(
                     RightSweepSetupEditSegmentInitListControlData,
                      new KeyValuePair<string, object>(RightSweepSetupEditSegmentInitListControlData, null)));

                    Thread.Sleep(2);
                }
            }));


            ThreadPool.QueueUserWorkItem(new WaitCallback(par =>
            {
                while (true)
                {
                    AddMessageQueues(new KeyValuePair<string, KeyValuePair<string, object>>(
                     RightSweepSetupEditSegmentInitListControlData,
                      new KeyValuePair<string, object>(RightSweepSetupEditSegmentInitListControlData, null)));

                    Thread.Sleep(3);
                }
            }));


            ThreadPool.QueueUserWorkItem(new WaitCallback(par =>
            {
                ExecuteInsideMessageQueues();
            }));
        }


        public static void Test()
        {
            TestConcurrentDictionary testConcurrentDictionary = new TestConcurrentDictionary();
        }

    }
}
